import logging

from livekit.agents import AutoSubscribe, JobContext, WorkerOptions, WorkerType, cli, JobRequest
from livekit import rtc
import time

logger = logging.getLogger("my-worker")
logger.setLevel(logging.INFO)


async def entrypoint(ctx: JobContext):
    logger.info("starting entrypoint")
    room = ctx.room
    await ctx.connect(auto_subscribe=AutoSubscribe.SUBSCRIBE_ALL)

    logger.info("connected to the room")
    # add your agent logic here!

    # receiving chat
    @ctx.room.on("data_received")
    def on_data_received(data: rtc.DataPacket):
        logging.info("received data from : %s", data.data)
        time.sleep(59)

async def request_fnc(ctx: JobRequest) -> None:
    await ctx.accept(
        # the agent's name (Participant.name), defaults to ""
        name="odd-cloud",
        # the agent's identity (Participant.identity), defaults to "agent-<jobid>"
        identity="agent-odd-"+ctx.id,
    ) 

if __name__ == "__main__":
    # WorkerType.ROOM is the default worker type which will create an agent for every room.
    # You can also use WorkerType.PUBLISHER to create a single agent for all participants that publish a track.
    cli.run_app(WorkerOptions(entrypoint_fnc=entrypoint, worker_type=WorkerType.ROOM, request_fnc=request_fnc))
